package com.gzsxy.esjy.service.product.controller.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gzsxy.esjy.service.base.DictionaryConstants;
import com.gzsxy.esjy.service.base.dto.response.*;
import com.gzsxy.esjy.service.product.entity.Product;
import com.gzsxy.esjy.service.product.entity.vo.ApiProductQueryVo;
import com.gzsxy.esjy.service.product.entity.vo.CategoryQueryVo;
import com.gzsxy.esjy.service.product.entity.vo.ProductQueryVo;
import com.gzsxy.esjy.service.product.service.CategoryService;
import com.gzsxy.esjy.service.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xiaolong
 * @version 1.0
 * @description: 前台商品
 * @date 2022/2/10 19:49
 */
@CrossOrigin  //解决跨域
@RefreshScope    //动态刷新配置文件-外部配置文件
@RestController
@RequestMapping("/api/product")
@Api("前台商品")
public class ApiProductController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    @ApiOperation("根据查询对象查询指定商品")
    public DefaultResponse getProductByType(ApiProductQueryVo apiProductQueryVo){
        List<ProductRsp> list = productService.getProductByType(apiProductQueryVo);
        return DefaultResponse.ok(list);
    }

    @GetMapping("/{id}")
    @ApiOperation("查询商品详情")
    public DefaultResponse productInfo(@PathVariable String id){
        ProductRsp productRsp = productService.getProductById(id);
        return DefaultResponse.ok(productRsp);
    }


    @ApiOperation("查询所有的分类")
    @GetMapping("/category")
    public DefaultResponse getAllcategory(){
        //与首页分类数保存一致，15条
        IPage<?> pageParam = new Page<>(1,15);
        CategoryQueryVo queryVo = new CategoryQueryVo();
        PageResult<CategoryRsp> result = categoryService.selectPage(pageParam, queryVo);

//        ApiCategoryResponse apiCategoryResponse = new ApiCategoryResponse();
//        //一级分类列表
//        List<CategoryRsp> level =  categoryService.getByLevel(DictionaryConstants.NUMBER_0);
//        //二级分类列表
//        List<CategoryRsp> level1 =  categoryService.getByLevel(DictionaryConstants.NUMBER_1);
//        //三级分类列表
//        List<CategoryRsp> level2 =  categoryService.getByLevel(DictionaryConstants.NUMBER_2);
//        //获取前15个分类
//        if (level.size()>=10){
//            level = level.subList(0, 10);
//        }
//        if (level1.size()>=10){
//            level1 = level1.subList(0, 10);
//        }
//        if (level2.size()>=10){
//            level2 = level2.subList(0, 10);
//        }
//        apiCategoryResponse.setLevel(level);
//        apiCategoryResponse.setLevel1(level1);
//        apiCategoryResponse.setLevel2(level2);
        return DefaultResponse.ok(result.getRecords());
    }
}
